<?php

class item implements observable{
	/**
	 * statyki 
	 */

	public static $itemCounter = 0;
	public static $allItems = array();

	public static function getItemById($id){
/*{{{*/
		return item::$allItems[$id];
/*}}}*/
	}

	public static function tick(){
/*{{{*/
		foreach(self::$allItems as $i){
			$i -> heartbeat();
		}
/*}}}*/
	}


	/**
	 * cechy klasy 
	 */
	public $symbol = 'I';

	/**
	 *  moze sie przydac
	 */

	protected $area;

	/**
	 * cechy itemu 
	 */
	protected $id = 0;
	protected $coord;

	public $age;

	public function __construct(){
/*{{{*/
		// obiekty
		$this -> area = area::getInstance();
		
		// zarejestruj sie
		$this -> id = item::$itemCounter ++;
		item::$allItems[$this -> id] = $this;

		
		// utworz cechy wspolne dla wszystkich itemow
		$this -> age = property::create('age', $this);

		$this -> initProperties();

		// pojaw sie gdzies na mapie
		$this -> area -> setMyRandomLocation($this);

/*}}}*/
	}

	protected function initProperties(){
	
	}

	public function heartbeat(){
/*{{{*/
		$bLive = true;

		$event = event::create('heartbeat');
		$this -> sendEvent($event);

		$this -> move();
		$this -> interact();
/*}}}*/
	}

	public function interact(){

	}


	public function move(){
/*{{{*/
		//$crd = $this -> getMove();
		//$this -> area -> setItemLocation($this, $crd, true);
/*}}}*/
	}


	public function endOfLife($sReason = ''){
/*{{{*/
		echo "item {$this -> id} dies: {$sReason}\n";
			
		$this -> area -> removeItem($this);
		unset(self::$allItems[$this -> id]);
		unset($this);
/*}}}*/
	}

	public function getId(){
/*{{{*/
		return $this -> id;
/*}}}*/
	}

	public function setCoord(coord $c){
/*{{{*/
		$this -> coord = clone $c;
/*}}}*/
	}

	public function getCoord(){
/*{{{*/
		return $this -> coord;
/*}}}*/
	}

	public function printInfo(){
/*{{{*/
		$table = new Console_Table();
		$table -> setHeaders(array('property', 'value'));

		$table -> addRow(array('id', $this -> id));
		$table -> addRow(array('age', $this -> age));

		echo $table -> getTable();
/*}}}*/
	}

	public function getMetric(){
/*{{{*/
		return "{$this -> symbol}[#{$this -> id};a={$this -> age -> value}]";
/*}}}*/
	}

	/**
	 * wszystko zwiazane ze zdarzeniami... 
	 */

	private $observers = array();

	public function registerObserver(observer $observer){
		$this -> observers[] = $observer;
	}

	public function sendEvent($event){

		foreach($this -> observers as $observer){
			$observer -> event($event);
		}

	}



	/**
	 * to be overriden 
	 */


	protected function getMove(){
/*{{{*/
/*}}}*/
	}


}


?>
